<?php

use app\index\model\FirClassification;
use app\index\model\Pictures;
use app\index\model\SecClassification;
use think\Db;

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
 * Jwt密钥
 */
function getJwtKey()
{
    return "wodeJwT123";
}

/*
 * 连接 sku_key sku_val spu_sku 三张表，获取标准商品的规格、颜色等属性
 */
function getSpuSku($id)
{
    return Db::table('spu_sku')
        ->alias('ss')
        ->join('sku_key sk', 'sk.id = ss.key_id')
        ->join('sku_val sv', 'sv.id = ss.val_id and sv.id = ss.val_id')
        ->where('ss.sid', $id)
        ->field('name,value')
        ->select();
}

function getGoodsByTypeWithPrice($page, $pageSize, $type, $minPrice, $maxPrice)
{
    $offset = $pageSize * ($page - 1);
    return Db::query("SELECT * FROM goods WHERE id IN (SELECT gid FROM spu WHERE price BETWEEN $minPrice AND $maxPrice)
                            AND goods.classification_id = $type LIMIT $offset, $pageSize");
}

function getGoodsWithPrice($page, $pageSize, $minPrice, $maxPrice)
{
    $offset = $pageSize * ($page - 1);
    return Db::query("SELECT * FROM goods WHERE id IN (SELECT gid FROM spu WHERE price BETWEEN $minPrice AND $maxPrice)
                            LIMIT $offset, $pageSize");
}

/*
 * 为查询的商品添加 sku 字段(某款商品所有的规格等属性) 以及 spu 字段(标准商品具体的规格)
 */
function setSpuAndSku($goods)
{
    $goods['spu'] = Db::table('spu')->where('gid', $goods['id'])->select();
    $temp = $goods['spu'];
    // 保存商品的规格、颜色等属性
    $sku = [];
    foreach ($temp as &$v) {
        // 连接查询查出标准产品对应的属性
        $spuSku = getSpuSku($v['id']);
        $res = [];
        // 构造 spu 和 sku 字段的结果
        foreach ($spuSku as $v2) {
            $res[$v2['name']] = $v2['value'];
            $sku[$v2['name']][] = $v2['value'];
        }
        $v['sku'] = $res;
    }
    $goods['spu'] = $temp;
    $goods['sku'] = $sku;
    return $goods;
}

/*
 * 为某商品查询相应的 sku 属性
 */
function setOrderSku($sid)
{
    $spuSku = getSpuSku($sid);
    $sku = [];
    foreach ($spuSku as $ss) {
        $sku[$ss['name']] = $ss['value'];
    }
    return $sku;
}

/*
* 商品图片上传函数，返回 url 数组
*/
function uploadPic()
{
    $pictures = input("file.pictures");
    $introducePic = input("file.introducePic");
    $urls = [];
    if ($pictures != '') {
        $info = $pictures->move("D:/phpstudy_pro/WWW/brotherhood/brother_hood/uploads/goods/", time() . $pictures->getInfo()['name']);
        if ($info) {
            $urls[] = [
                'url' => "http://10.61.22.44:8889/brother_hood/uploads/goods/" . $info->getFileName(),
                'type' => 0
            ];
        }
    }
    if ($introducePic != '') {
        foreach ($introducePic as $f2) {
            $info = $f2->move("D:/phpstudy_pro/WWW/brotherhood/brother_hood/uploads/goods/", time() . $f2->getInfo()['name']);
            if ($info) {
                $urls[] = [
                    'url' => "http://10.61.22.44:8889/brother_hood/uploads/goods/" . $info->getFileName(),
                    'type' => 1
                ];
            }
        }
        unset($f2);
    }
    return $urls;
}

/*
 * 用户头像上传
 */
function uploadAvatar($name)
{
    $pic = input("file.$name");
    $url = '';
    if ($pic != null) {
        $info = $pic->move("D:/phpstudy_pro/WWW/brotherhood/brother_hood/uploads/avatar/", time() . $pic->getInfo()['name']);
        if ($info) {
            $url = "http://10.61.22.44:8889/brother_hood/uploads/avatar/" . $info->getFileName();
        }
    }
    return $url;
}

/*
 * 此处获取商品对应的规格、颜色等属性，同时获取预览图
 */
function setPicAndSpuAndSku($goods)
{
    foreach ($goods as &$value) {
        $value['picture'] = Pictures::where(['gid' => $value['id'], 'type' => 0])->find();
        $value['detailPic'] = Pictures::where(['gid' => $value['id'], 'type' => 1])->select();
        $value['secCla'] = SecClassification::where('id', $value['classification_id'])->select()[0];
        $value['firCla'] = FirClassification::where('id', $value['secCla']['pid'])->value('name');
        $value = setSpuAndSku($value);
    }
    return $goods;
}